package com.icebergteam.warfacewidget.core.settings;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.R;
import com.icebergteam.warfacewidget.core.LogManager;

public class Top100Settings extends Settings {
	public static final String className = Top100Settings.class.getCanonicalName();
	
	public final static String PREFS_FILE_NAME = "top100_settings.ini";
	
	// Response
	
	private int resultItemsCount = 10;
	private int currentIndexServer = 0;
	
	int offsetIndexCategory = 1;
	private int currentIndexCategory = offsetIndexCategory;
	
	private int currentIndexClass = 0;
	
	private long stepUpdateTop100RankActivity = 3600000;
	
	
	public int getResultItemsCount() {
		return resultItemsCount;
	}
	public void setResultItemsCount(int resultItemsCount) {
		this.resultItemsCount = resultItemsCount;
	}
	
	
	public Top100Settings() {
		Context context = AppManager.getContext();
		init(context);
		load();
	}
	
	

	@Override
	public void save() {
		Context context = AppManager.getContext();
		try {
			FileOutputStream fos = context.openFileOutput(getPrefFileName(),
					Context.MODE_PRIVATE);
		
			this.properties.setProperty(
					getKey(R.string.key_prefs_finder_response_result_items_count),
					String.format("%s", this.resultItemsCount));
			this.properties.setProperty(
					getKey(R.string.key_prefs_top100_current_index_server),
					String.format("%s", this.getCurrentIndexServer()));
			this.properties.setProperty(
					getKey(R.string.key_prefs_top100_current_index_category),
					String.format("%s", this.getCurrentIndexCategory()));
			this.properties.setProperty(
					getKey(R.string.key_prefs_top100_current_index_class),
					String.format("%s", this.getCurrentIndexClass()));
			this.properties.setProperty(
					getKey(R.string.key_prefs_top100_step_update_top100_rankactivity),
					String.format("%s", this.getStepUpdateTop100RankActivity()));
			
			
		
			this.properties.store(fos, "warfacewidget");
			fos.close();
			LogManager.LogV(Top100Settings.className, LogManager.getExecuteMethodName(), this.resultItemsCount, this.getCurrentIndexServer(), this.getCurrentIndexCategory(), this.getCurrentIndexClass(), this.getStepUpdateTop100RankActivity(), this.properties.toString());
		} catch (FileNotFoundException e) {
			LogManager.LogE(Top100Settings.className, LogManager.getExecuteMethodName() + " can't read pref file",
					e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			LogManager.LogE(Top100Settings.className, LogManager.getExecuteMethodName() + " can't read pref file",
					e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			LogManager.LogE(Top100Settings.className, LogManager.getExecuteMethodName(),
					e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	public void load() {
	
		String resultItemsCount = this.properties.getProperty(
				getKey(R.string.key_prefs_finder_response_result_items_count),
				String.format("%s", this.resultItemsCount));
		String currentIndexServer = this.properties.getProperty(
				getKey(R.string.key_prefs_top100_current_index_server),
				String.format("%s", this.currentIndexServer));
		String currentIndexCategory = this.properties.getProperty(
				getKey(R.string.key_prefs_top100_current_index_category),
				String.format("%s", this.currentIndexCategory));
		String currentIndexClass = this.properties.getProperty(
				getKey(R.string.key_prefs_top100_current_index_class),
				String.format("%s", this.currentIndexClass));
		String stepUpdateTop100RankActivity = this.properties.getProperty(
				getKey(R.string.key_prefs_top100_step_update_top100_rankactivity),
				String.format("%s", this.stepUpdateTop100RankActivity));
		
		
		try {
			
			setResultItemsCount(Integer.parseInt(resultItemsCount));
			setCurrentIndexServer(Integer.parseInt(currentIndexServer));
			setCurrentIndexCategory(Integer.parseInt(currentIndexCategory));
			setCurrentIndexClass(Integer.parseInt(currentIndexClass));
			setStepUpdateTop100RankActivity(Long.parseLong(stepUpdateTop100RankActivity));

			LogManager.LogV(Top100Settings.className, LogManager.getExecuteMethodName(), this.resultItemsCount, getCurrentIndexServer(), getCurrentIndexCategory(), getCurrentIndexClass(), getStepUpdateTop100RankActivity(), this.properties.toString());
		} catch (Exception e) {
			LogManager.LogE(Top100Settings.className, LogManager.getExecuteMethodName(), e.getMessage());
			e.printStackTrace();
		}
	}
	/**
	 * @return the currentIndexServer
	 */
	public int getCurrentIndexServer() {
		return currentIndexServer;
	}
	/**
	 * @param currentIndexServer the currentIndexServer to set
	 */
	public void setCurrentIndexServer(int currentIndexServer) {
		this.currentIndexServer = currentIndexServer;
	}
	/**
	 * @return the currentIndexCategory
	 */
	public int getCurrentIndexCategory() {
		return currentIndexCategory;
	}
	/**
	 * @param currentIndexCategory the currentIndexCategory to set
	 */
	public void setCurrentIndexCategory(int currentIndexCategory) {
		this.currentIndexCategory = currentIndexCategory;
	}
	/**
	 * @return the currentIndexClass
	 */
	public int getCurrentIndexClass() {
		return currentIndexClass;
	}
	/**
	 * @param currentIndexClass the currentIndexClass to set
	 */
	public void setCurrentIndexClass(int currentIndexClass) {
		this.currentIndexClass = currentIndexClass;
	}
	/**
	 * @return the stepUpdateTop100RankActivity
	 */
	public long getStepUpdateTop100RankActivity() {
		return stepUpdateTop100RankActivity;
	}
	/**
	 * @param stepUpdateTop100RankActivity the stepUpdateTop100RankActivity to set
	 */
	public void setStepUpdateTop100RankActivity(long stepUpdateTop100RankActivity) {
		this.stepUpdateTop100RankActivity = stepUpdateTop100RankActivity;
	}
	@Override
	public String getPrefFileName() {
		return Top100Settings.PREFS_FILE_NAME;
	}
}